package com.lanou3g.hw0420;

import java.io.*;

/**
 * Created by dllo on 18/4/20.
 */
public class Copy {
    public static void main(String[] args) {

    }
    public static void copyByBuffer(String srcPath,String desPath){
        File srcFile= new File(srcPath);//srcPath 来源路径
        File desFile= new File(desPath);
        FileInputStream fis =null;//FileInputStream  文件输入流  字节的输入流
        FileOutputStream fos =null;// FileOutputStream 文件输出流  字节的输出流
        BufferedInputStream bis =null;//缓冲输入流
        BufferedOutputStream bos =null;//


        try {
            desFile.createNewFile();
            fis= new FileInputStream(srcFile);
            fos =new FileOutputStream(desFile);
            bis=new BufferedInputStream(fis);
            bos=new BufferedOutputStream(fos);
            int pos=0;
            byte[]buf=new byte[1024];
            while ((pos=bis.read(buf))!=-1){
                bos.write(buf,0,pos);
            }
            bos.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public static void copy(String srcPath,String desPath){
        File srcFile= new File(srcPath);
        File desFile= new File(desPath);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos =null;


        try {
            desFile.createNewFile();
            fis= new FileInputStream(srcFile);
            fos= new FileOutputStream(desFile);
            bis=new BufferedInputStream(fis);
            bos=new BufferedOutputStream(fos);
            byte[]buf=new byte[1024];
            int pos=0;
            while ((pos=bis.read(buf))!=-1){
                bos.write(buf,0,pos);
            }
            bos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos!=null){

                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static void fun(String srcPath,String desPath){
        File srcFile= new File(srcPath);
        File desFile= new File(desPath);
        FileInputStream fis=null;
        FileOutputStream fos =null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos =null;

        try {
            desFile.createNewFile();
            bis= new BufferedInputStream(new FileInputStream(srcFile));
            bos=new BufferedOutputStream(new FileOutputStream(desFile));
            int pos=0;
            byte[]buf=  new byte[1024];
            while ((pos= bis.read(buf))!=-1){
                bos.write(buf,0,pos);

            }
            bos.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
